package com.wf.ew.system.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.ServiceSupport;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.biz.DictionarydataBiz;
import com.wf.ew.system.model.Dictionarydata;
import com.wf.ew.system.model.User;
import com.wf.ew.system.service.DictionarydataService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-08-06 16:09:47
 * @version ：1.0
 */
@Service
public class DictionarydataServiceImpl extends ServiceSupport implements DictionarydataService {

	@Autowired
	private DictionarydataBiz dictionarydataBiz;

	@Override
	public String list(PageResult<T> page, Dictionarydata dictionarydata) {
		return dictionarydataBiz.list(page,dictionarydata);
	}

	@Override
	public List<Dictionarydata> list(Dictionarydata dictionarydata) {
		return dictionarydataBiz.list(dictionarydata);
	}

	@Override
	public Dictionarydata getById(Dictionarydata dictionarydata) {
		return dictionarydataBiz.getById(dictionarydata);
	}

	@Transactional
	@Override
	public JsonResult add(Dictionarydata dictionarydata, User loginUser) {
		if (dictionarydataBiz.add(dictionarydata, loginUser)) {
			return JsonResult.ok("添加成功");
		}
		return JsonResult.error("添加失败");
	}

	@Transactional
	@Override
	public JsonResult update(Dictionarydata dictionarydata, User loginUser) {
		if (dictionarydataBiz.update(dictionarydata, loginUser)) {
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Transactional
	@Override
	public JsonResult delete(Dictionarydata dictionarydata, User loginUser) {
		if (dictionarydataBiz.delete(dictionarydata, loginUser)) {
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}

	@Override
	@Transactional
	public JsonResult dictdata_code_unique(Dictionarydata dictionarydata) {
		String id = dictionarydata.getId();
		String dictdata_code = dictionarydata.getDictdata_code();
		dictionarydata = dictionarydataBiz.getById(dictionarydata);
		if(!Utils.isNotEmpty(id)){
			if(dictionarydata==null){
				return JsonResult.ok("可以使用字典主键");
			} else {
				return JsonResult.error("请保持字典主键唯一性");
			}
		} else {
			if(dictionarydata==null){
				Dictionarydata dictionarydata2 = new Dictionarydata();
				dictionarydata2.setDictdata_code(dictdata_code);
				dictionarydata = dictionarydataBiz.getById(dictionarydata2);
				if(dictionarydata==null){
					return JsonResult.ok("可以使用字典主键");
				} else {
					return JsonResult.error("请保持字典主键唯一性");
				}
			} else {
				return JsonResult.ok("可以使用字典主键");
			}

		}
	}
}
